//渲染顾客信息
function showCustmoer() {
    Ajax({
        url: 'http://localhost/hotel/php/custmoer.php',
        success(msg) {
            sessionStorage.setItem("custmoer", msg);
            sessionStorage.setItem("page",1)
            msg = JSON.parse(msg).slice(0,8);
            custmoers(msg);
            pages();
        }
    })
}
showCustmoer();

function custmoers(msg) {
    const tbody = $("tbody");
    tbody.html(" ");
    msg.forEach(item => {
        let str = `
    <tr>
              <td>${item.idCard}</td>
              <td>${item.name}</td>
              <td>${item.gender}</td>
              <td>${item.phone}</td>
              <td>${item.adress}</td>
              <td>
                <button type="button"  data-upd="change"  class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#myModal" id="${item.userId}">修改</button>
                <button type="button" id="del" data-cid=${item.userId} class="btn btn-danger">删除</button>
              </td>
            </tr>
    `
        tbody[0].innerHTML += str;
    });
}

//添加顾客
function addcus() {
    const cuscid = $('#cuscid').val();
    const cusname = $('#cusname').val();
    const cusphone = $('#cusphone').val();
    const cusadress = $('#cusadress').val();
    let cusgender;
    if ($('.ssex')[0].checked == true) {
        cusgender = '男'
    } else {
        cusgender = '女'
    }

    $.ajax({
        type:'POST',
        url: 'http://localhost/hotel/php/addcustomer.php',
        data: {
            cuscid,
            cusname,
            cusgender,
            cusphone,
            cusadress
        },
        success() {
            alert('添加成功');
            location.reload()
        }
    })

}


//删除操作
//利用事件委托
function cust(){
$('tbody').click(function (e) {
    //删除操作
    //console.log(e.target.dataset.cid);
    //console.log(e.target);
   
    if (e.target.id == 'del') {
        let id = e.target.dataset.cid;
        let chose = confirm('确定删除吗？');
        if (chose) {
            $.ajax({
                type: "POST",
                url: "http://localhost/hotel/php/deleteCustmoer.php",
                data: { id },
                success() {
                    alert("删除成功!");
                    location.reload();
                }
            })
        }
    }
    //渲染修改页面的数据
    if (e.target.dataset.upd == 'change') {
        let cid = e.target.id;
        $.ajax({
            type: "POST",
            url: 'http://localhost/hotel/php/customerone.php',
            data: { id: cid },
            success(msg) {
                const customerone = JSON.parse(msg);
                $('#idcard').attr("cid", cid)
                $('#idcard').val(customerone.idCard);
                $('#cname').val(customerone.name);
                $('#cphone').val(customerone.phone);
                $('#roomAdress').val(customerone.adress)
                let gender = customerone.gender;
                if (gender == '男') {
                    $('.cgender')[0].checked = true;
                } else {
                    $('.cgender')[1].checked = true;
                }
            }
        })
    }
})
}
cust();

//修改信息
function cusUpdate() {
    const idCard = $('#idcard').val();
    const cname = $('#cname').val();
    const phone = $('#cphone').val();
    const adress = $('#roomAdress').val();
    const cgender = $('.cgender');
    const id = $('#idcard').attr('cid');
    let gender;
    if (cgender[0].checked == true) {
        gender = '男';
    } else {
        gender = '女';
    }

    $.ajax({
        type: "POST",
        url: 'http://localhost/hotel/php/updatecustomer.php',
        data: {
            idCard,
            cname,
            gender,
            phone,
            id,
            adress
        },
        success() {
            location.reload();
        }
    })

}



//分页
function first(){
    sessionStorage.setItem("page",1);
    let custmoer=sessionStorage.getItem("custmoer");
    custmoer=JSON.parse(custmoer).slice(0,8);
    custmoers(custmoer);
    pages()
   
}

function last(){
    let custmoer=sessionStorage.getItem("custmoer");
    custmoer=JSON.parse(custmoer);
    const p=Math.ceil(custmoer.length/8);
    sessionStorage.setItem('page',Math.ceil(custmoer.length/8));
    custmoer=custmoer.slice(Math.floor(custmoer.length/8)*8,custmoer.length);
    sessionStorage.setItem('page',p)
    custmoers(custmoer);
    pages()
   
}
function next(){
    let p=sessionStorage.getItem('page')-0;
    p=p+1;
    let c=sessionStorage.getItem('custmoer');
    c=JSON.parse(c);
    if(p>Math.ceil(c.length/8)) p=Math.ceil(c.length/8);
    sessionStorage.setItem('page',p);
    c=c.slice((p-1)*8,p*8);
    custmoers(c);
    
    pages()
}
function up(){
    let p=sessionStorage.getItem('page')-0;
    p=p-1;
    let c=sessionStorage.getItem("custmoer");
    c=JSON.parse(c)
    if(p<1) p=1;
    sessionStorage.setItem('page',p);
    c=c.slice((p-1)*8,p*8);
    custmoers(c);
  
    pages()
}

$('#pages').html(sessionStorage.getItem('page'));

function pages(){
   let pages= sessionStorage.getItem('page');
   $('#pages').html(pages);
   let maxpages=sessionStorage.getItem('custmoer');
   maxpages= Math.ceil(((JSON.parse(maxpages)).length) /8);
   //console.log(pages, maxpages);
   if(maxpages==1){
    $('#first').attr('disabled',true);
    $('#up').attr('disabled',true);
    $('#last').attr('disabled',true);
    $('#next').attr('disabled',true);
    
   }

   if(pages==1){
   $('#first').attr('disabled',true);
   $('#up').attr('disabled',true);
   }else{
    $('#first').attr('disabled',false);
    $('#up').attr('disabled',false);
   }

   if(pages==maxpages){
    $('#last').attr('disabled',true);
    $('#next').attr('disabled',true);
    }else{
     $('#last').attr('disabled',false);
     $('#next').attr('disabled',false);
   }
}

//按性别查询
function getGender(sex){
   $.ajax({
        url:'http://localhost/hotel/php/cusgender.php',
        data:{
            gender:sex
        },
        success(msg){
            sessionStorage.setItem("custmoer",msg)
            msg=JSON.parse(msg);
            custmoers(msg.slice(0,8));
            pages();
        }
    })
}
//模糊查找
function findName(){
    let name=$("#cuName").val()
    console.log(name);
    $.ajax({
        type:'POST',
        url:"http://localhost/hotel/php/findName.php",
        data:{name},
        success(msg){
            sessionStorage.setItem("custmoer",msg);
            msg=JSON.parse(msg);
            custmoers(msg);
            pages();
        }
    })
}


